/**
 * Created by tanbingqin on 2019/11/22.
 */
import { messages } from "share/common";

export default {
  //返回companyList的name str
  getCompanyName(customFormCompanyRequest) {
    let companyList = customFormCompanyRequest ? customFormCompanyRequest.companyList : [];
    let nameList = [];
    companyList && companyList.length && companyList.forEach(company => {
      nameList.push(company.name);
    });
    return nameList.length ? nameList.join(',') : messages('request-2.key4')/*通用*/;
  },

  //判断item是否包含在list中
  isContainDefault(list, item, key) {
    let isContain = false;
    !item[key] && (isContain = true);
    list.map(selected => {
      if (selected[key] === item[key]) {
        isContain = true;
      }
    });
    return isContain;
  },

  //根据customFormPropertyMap，返回需要setState的{}key值以及value值，有特殊逻辑的得在外层额外处理，比如json封装的多个key，额外调接口查询表单名称等等
  getStateKey(propertyKey, propertyValue) {
    let mapping = {
      'budget.er.enabled': {
        isBudgetEr: propertyValue === 'true',
        isBudgetCa: propertyValue === 'true'
      },
      'public.shop.application': {isPurchaseApplication: propertyValue === 'true'},
      'from.travel.relevance.application': {isRelatedMore: (!propertyValue || propertyValue === 'Y') ? 'Y' : 'N'},
      'from.expense.relevance.application': {isRelatedMore: (!propertyValue || propertyValue === 'Y') ? 'Y' : 'N'},
      'from.application.relevance.loan': {isLoanRelatedMore: (!propertyValue || propertyValue === 'Y') ? 'Y' : 'N'},
      'application.property.deliver.others': {isDeliverOther: propertyValue === 'true'},
      'report.property.create.others.expense': {createOtherExpense: propertyValue === 'true'},
      'report.property.pay.multi.lines': {isMultiLine: propertyValue === 'true'},
      'application.property.related.mutil.applications': {isRelatedNum: propertyValue === 'Y' ? 'Y' : 'N'},
      'application.funds': {isApplicationFund: propertyValue === 'true'},
      'application.direct.loan.enabled': {isAutoLoan: propertyValue === 'true'},
      'report.property.associated.contract.enabled': {contractEnable: propertyValue === 'true'},
      'loan.property.associated.contract.enabled': {contractEnable: propertyValue === 'true'},
      'associate.contract.required': {contractRequired: propertyValue === 'true'},
      'report.property.associated.contract.config': {contractLocation: propertyValue},
      'import.unify.data': {isImportData: propertyValue === 'true'},
      'document.venmasteradd.enable': {addVenMaster: propertyValue},
      'report.pay.delay': {payDelay: propertyValue === 'true'},
      'multi.loan': {multiLoanEnable: propertyValue === 'true'},
      'document.payee.not.bring': {isNotPayeeBring: propertyValue === 'true'},
      'document.payee.not.changeable': {isNotPayeeEdit: propertyValue === 'true'},
      'application.property.customize.expense.type.enable': {isAlreadyNew: propertyValue === 'true'},
      'corp.payment.available.type': { availableType: propertyValue || [] },
      'payment.method': { paymentMethod: propertyValue },
      'related.application.property': {
        applicationEnabled: !!(propertyValue && propertyValue.enable),
        requestFormType: {
          range: (!propertyValue || !propertyValue.formDataList || !propertyValue.formDataList.length) ? 'ALL' : 'PARTIAL',
          value: propertyValue ? (propertyValue.formDataList || []) : []
        },
        requestFormTypeRangeDisabled: !propertyValue || !propertyValue.formDataList || !propertyValue.formDataList.length,
        relatedApplicationScope: propertyValue && propertyValue.relatedApplicationScope,
        relatedApplicationRequired: !!(propertyValue && propertyValue.required)
      },
      'prepayment.type': {
        prepaymentType: {
          range: (!propertyValue || !propertyValue.prepaymentTypeList || !propertyValue.prepaymentTypeList.length) ? 'ALL' : 'PARTIAL',
          value: propertyValue ? (propertyValue.prepaymentTypeList || []) : []
        },
        prepaymentTypeRangeDisabled: !propertyValue || !propertyValue.prepaymentTypeList || !propertyValue.prepaymentTypeList.length
      },
      'prepayment.writeoff': {
        writeOffEnabled: !!(propertyValue && propertyValue.enable),
        writeOffWithDepartment: !!(propertyValue && propertyValue.writeOffWithDepartment),
        expenseTypeScope: {
          range: (!propertyValue || !propertyValue.expenseTypeScope || !propertyValue.expenseTypeScope.length) ? 'ALL' : 'PARTIAL',
          value: propertyValue ? (propertyValue.expenseTypeScope || []) : []
        }
      }
    };
    return mapping[propertyKey];
  },

  //根据customFormPropertyMap，返回需要setFieldsValue的{}key值以及value值
  getFieldKey(propertyKey, propertyValue) {
    let mapping = {
      'loan.application.participation.repay.enable': {associatePayExpense: propertyValue === 'true'},
      'loan.bill.pay.type': {paymentType: propertyValue},
      'payee.type': {payeeType: propertyValue ? propertyValue.split(',') : ["1", "2"]},
      'application.property.customize.expense.type.enable': {newTravel: propertyValue === 'true'},
      'loan.bill.pay.method.changeable': { loanType: propertyValue === 'true' }
    };
    return mapping[propertyKey];
  },

  //把zh_cn转换成zh_CN
  processLanguageToUpperCase(i18nList) {
    let i18nResultList = JSON.parse(JSON.stringify(i18nList));
    i18nResultList.map(item => {
      if (item.language === 'zh_cn') {
        item.language = 'zh_CN';
      }
    });
    return i18nResultList;
  },

  //把zh_cn转换成zh_cn
  processLanguageToLowerCase(i18nList) {
    let i18nResultList = JSON.parse(JSON.stringify(i18nList));
    i18nResultList.map(item => {
      if (item.language === 'zh_CN') {
        item.language = 'zh_cn';
      }
    });
    return i18nResultList;
  },

  //生成4位随机数
  s4(){
    return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
  },
  //生成uuid
  generateUid(){
    return (this.s4()+this.s4()+"-"+this.s4()+"-"+this.s4()+"-"+this.s4()+"-"+this.s4()+this.s4()+this.s4());
  },

  generateFormCode(formType, setOfBooksCode) {
    let result = setOfBooksCode ? setOfBooksCode + '_' : '';
    let randomStr = Math.floor(Math.random()*9000 + 1000).toString();
    switch (formType) {
      case 2001:
        result += 'T_REQUEST_' + randomStr;
        break;
      case 2002:
        result += 'E_REQUEST_' + randomStr;
        break;
      case 3001:
        result += 'G_EXPENSE_' + randomStr;
        break;
      case 2006:
        result += 'CASH_ADV_' + randomStr;
        break;
      case 2201:
        result += 'P_REQUEST_' + randomStr;
        break;
      case 3004:
        result += 'VENDOR_PAYMENT_' + randomStr;
        break;
    }
    return result;
  },

  generateFormSequence(formType) {
    let sequence = 10;
    switch (formType) {
      case 3001:
        sequence = 30;
        break;
      case 2006:
        sequence = 40;
        break;
      case 2201:
        sequence = 70;
        break;
    }
    return sequence;
  },

  //处理布局的控件列表的初始化
  processLayoutWidget(key, subKey, resultKey, widgetList) {
    let keyIndex = -1;
    let subKeyIndex = -1;
    widgetList && widgetList.forEach((widget, widgetIndex) => {
      if(widget.messageKey === 'select_cost_center' && key === 'select_cost_center'){
        let dataSource = widget.dataSource ? JSON.parse(widget.dataSource) : '';
        if (dataSource.type === 'father') {
          keyIndex = widgetIndex;
        }
        if (dataSource.type === 'son') {
          subKeyIndex = widgetIndex;
        }
      } else {
        if (widget.messageKey === key) {
          keyIndex = widgetIndex;
        }
        if (widget.messageKey === subKey) {
          subKeyIndex = widgetIndex;
        }
      }
    });
    if (keyIndex > -1 && subKeyIndex > -1) {
      widgetList[keyIndex].messageKey = resultKey;
      widgetList[keyIndex].subWidget = JSON.parse(JSON.stringify(widgetList[subKeyIndex]));
      widgetList.splice(subKeyIndex, 1);
    }
  }
}
